home *** CD-ROM | disk | FTP | other *** search
- Path: netaxs.com!not-for-mail
- From: grulm@netaxs.com (J. A. McNamara)
- Newsgroups: comp.lang.c
- Subject: binary tree question
- Date: 21 Mar 1996 21:11:15 GMT
- Organization: Philadelphia's Complete Internet Provider
- Message-ID: <4isglj$cgg@netaxs.com>
- NNTP-Posting-Host: unix3.netaxs.com
- X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
-
- I'm willing to bet that the answer to this is obvious, but I'm trying to
- free each node of a binary tree as it is read -- preferably *after* it's
- been read :) the following code doesn't appear to free the memory, but I
- haven't figured out how to prod my debugger into telling me how much memory
- is being used, so I'm not so sure . . . (by the way, it gives me no
- problems in any other respect). (I'd also like to mention that this is
- *not* homework)
-
- /***********************************************************************/
- /*link is the linked list struct type; tree_n is the tree node struct type*/
- /* addlink() works fine, too, and both are prototyped. */
-
- link *inorder(tree_n *tn, link *tail, int *i) /* read tree into list */
- {
- if (tn != NULL) {
- tail = inorder(tn->l, tail, i);
- tail = addlink(tail, tn->f); /* hand data ptr to list */
- (*i)++; /* keep track of total */
- tail = inorder(tn->r, tail, i);
- }
- if (tn!=NULL) free(tn); tn=NULL;
- return tail;
- }
- /**************************************************************************/
-
- I also tried the following:
-
- free(tn->l); tn->l=NULL;
- free(tn->r); tn->r=NULL;
-
- . . . which gave me some serious garbage, though I'm not quite sure why.
-
- any help would be appreciated. thanks in advance.
-
- --
- j a mcnamara aramancm a j
- grulm@netaxs.com moc.sxaten@mlurg
-